package com.example.myblog.controller;

import com.example.myblog.domain.Article;
import com.example.myblog.domain.Blog;
import com.example.myblog.domain.User;
import com.example.myblog.service.article.ArticleServiceImpl;
import com.example.myblog.service.blog.BlogService;
import com.example.myblog.service.user.UserServiceImpl;
import com.example.myblog.util.COOKIEUtil;
import com.example.myblog.util.RequestHttp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Wu YunXia
 * @Description: 交互类 文章
 * @Date: Created in 16:01 2021/08/30
 * @Modefied By:
 */

@RestController
@RequestMapping("/article")
public class ArticleController {


        @Resource
        HttpServletRequest request;
        @Resource
        HttpServletResponse response;



        @Resource
        private BlogService blogService;

        @Resource
        private UserServiceImpl userService;
        @Resource
        private ArticleServiceImpl articleService;

        Map map = new HashMap();


    /**
     *  cookie状态
     */
    private String getCookie(){
        String i = null;

        Cookie cookie = COOKIEUtil.getCookieByName(request,"ps");
        Cookie limitCookie = COOKIEUtil.getCookieByName(request,"limitName");
        System.out.println("cookie:"+cookie);
        System.out.println("limitCookie:"+limitCookie);
        if(cookie!=null){
            String uuid = cookie.getValue();
            String limits = limitCookie.getValue();
            System.out.println("limits:"+limits);

            if(uuid==null || limits==null){
                map.put("code", 13);
                map.put("msg", "请登录");
            }else{
                List<User> list = userService.queryForUuid(uuid);
                System.out.println("list:"+list);

                if(list.size()!=0){
                    int uid = list.get(0).getId();

                    String trueLimits = list.get(0).getLimits();

                    System.out.println("trueLimits:"+trueLimits);
                    if(uid>0) {
                        i = trueLimits;
                    }else{
                        map.put("code", 13);
                        map.put("msg", "用户不存在");
                    }
                }else{
                    map.put("code", 13);
                    map.put("msg", "登录过期");
                }

            }
        }else{
            map.put("code", 13);
            map.put("msg", "请登录");
        }


        return i;
    }




    /**
     * @desc: 添加
     */
    @RequestMapping(value = "/addArticle",method = RequestMethod.POST)
    public Object addBlog(@RequestBody Article article){

        String value = this.getCookie();
        if(value==null) return map;
        String[] lists = value.split("-");

        /**
         * 超  改
         */
        if(Arrays.asList(lists).contains("0") || Arrays.asList(lists).contains("2")) {

            int add = articleService.addArticle(article);
            if (add != 0) {
                map.put("code", 200);
                map.put("msg", "添加成功");
            } else {
                map.put("code", -1);
                map.put("msg", "添加失败");
            }

        }

        return map;
    }

    /**
     * @desc: 编辑  /修改
     */
    @RequestMapping(value = "/editArticle",method = RequestMethod.POST)
    public Object editArticle(@RequestBody Article article){

        String value = this.getCookie();

        if(value==null) return map;

        String[] lists = value.split("-");

        /**
         * 超  改
         */
//        if(Arrays.asList(lists).contains("0") || Arrays.asList(lists).contains("2")) {

            int edit = articleService.editArticle(article);
            if (edit != 0) {
                map.put("code", 200);
                map.put("msg", "修改成功");
            } else {
                map.put("code", -1);
                map.put("msg", "修改失败");
            }

//        }

        return map;
    }



    /**
     * @desc: 根据id查询
     */
    @RequestMapping( "/getArticleById")
    public Object getArticleById(@RequestParam int id){
        System.out.println("id:"+id);

        Article article = articleService.findById(id);
        if(article.getId()!=0){
            map.put("code",200);
            map.put("msg","查询成功");
            map.put("list",article);
        }else{
            map.put("code",-1);
            map.put("msg","查询失败");
        }


        return map;
    }

    /**
     * @desc: 模糊搜索
     */
    @RequestMapping( "/getArticleByKeyword")
    public Object getArticleByKeyword(@RequestParam String keyword){
        System.out.println("id:"+keyword);

        List<Article> article = articleService.getArticleByKeyword(keyword);
        System.out.println("article:"+article);
        if(article!=null){
            map.put("code",200);
            map.put("msg","查询成功");
            map.put("list",article);
        }else{
            map.put("code",-1);
            map.put("msg","查询失败");
        }


        return map;
    }


    /**
     * @desc: 查询全部
     */
    @RequestMapping("/getArticleAll")
    public Object selectArticleAll(){
//        String value = this.getCookie();
//        if(value==null) return map;

        List<Article> article = articleService.findAll();
        if (article.size() != 0) {
            map.put("code", 200);
            map.put("msg", "查询成功");
            map.put("list", article);
        } else {
            map.put("code", -1);
            map.put("msg", "查询失败");
        }


        return map;
    }



    /**
     * @desc: 删除  单个/全部
     */
    @RequestMapping("/delArticle")
    @ResponseBody
    public Object delArticle(@RequestBody Article article){
//        int uid = this.getCookie();//

        String value = this.getCookie();

        if(value==null) return map;

        String[] lists = value.split("-");



        /**
         * 超 或 删
         */
        if(Arrays.asList(lists).contains("0") || Arrays.asList(lists).contains("2")) {

            int id = article.getId();

            System.out.println("id:"+id);
            if (id > 0) { //删除单个
                boolean result = articleService.deleteById(id);
                String str = "删除失败";
                int code = -1;
                if (result) {
                    str = "删除成功";
                    code = 200;
                }

                map.put("msg", str);
                map.put("code", code);
            } else {  //删除全部
                boolean resultAll = articleService.deleteAll();
                String str = "删除失败";
                int code = -1;
                if (resultAll) {
                    str = "删除成功";
                    code = 200;
                }

                map.put("msg", str);
                map.put("code", code);
            }
        }

        return map;
    }



    }
